package com.sinsoft.monitorDemo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinsoft.monitorDemo.dao.MysqlInfoDao;
import com.sinsoft.monitorDemo.dao.NginxInfoDao;
import com.sinsoft.monitorDemo.dao.OracleInfoDao;
import com.sinsoft.monitorDemo.dao.ServerDao;
import com.sinsoft.monitorDemo.dao.TomcatInfoDao;
import com.sinsoft.monitorDemo.entity.MysqlInfo;
import com.sinsoft.monitorDemo.entity.NginxInfo;
import com.sinsoft.monitorDemo.entity.OracleInfo;
import com.sinsoft.monitorDemo.entity.ServerInfo;
import com.sinsoft.monitorDemo.entity.TomcatInfo;


/**
 * 各系统状态控制类（连接数 ，服务状态，端口状态）
 * @author 刘鹏博
 */
@Controller
@RequestMapping("/connInfo")
public class ConnNumController {
	
	
	/**
	 * 得到所有的应用连接数
	 * @return
	 */
	@RequestMapping(value = "/getAllConnNumList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getAllConnNumList(){
		ServerDao serverDao = new ServerDao();
		TomcatInfoDao tomcatInfoDao = new TomcatInfoDao();
		NginxInfoDao nginxInfoDao = new NginxInfoDao();
		MysqlInfoDao mysqlInfoDao = new MysqlInfoDao();
		OracleInfoDao oracleInfoDao = new OracleInfoDao();
		List<ServerInfo> serverInfos = serverDao.getAllServerInfo();
		List<Long> timeList = new ArrayList<Long>();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> maxConnMap = new HashMap<String, Object>();
		for (ServerInfo serverInfo : serverInfos) {
			if (serverInfo.getCode().contains("TCM")) {
				if(serverInfo.getIp().contains(",")){
					String[] split = serverInfo.getIp().split(",");
					List<TomcatInfo> tomcatInfoList = tomcatInfoDao.getTomcatInfoList(split[0], serverInfo.getCode());
					Long maxTime = tomcatInfoList.get(tomcatInfoList.size()-1).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						List<TomcatInfo> tomcatInfos = tomcatInfoDao.getTomcatInfoList(split[i], serverInfo.getCode());
						Long time = tomcatInfos.get(tomcatInfos.size()-1).getTime();
						if(maxTime<time){
							maxTime=time;
							j=i;
						}
					}
					List<TomcatInfo> tomcatInfos = tomcatInfoDao.getTomcatInfoList(split[j], serverInfo.getCode());
					List<Integer> conns = new ArrayList<Integer>();
					for (TomcatInfo tomcatInfo : tomcatInfos) {
						conns.add(tomcatInfo.getConnections());
					}
					map.put(serverInfo.getName(), conns);
					maxConnMap.put(serverInfo.getName(), tomcatInfoDao.getNewTomcatInfo(split[j], serverInfo.getCode()).getMaxConnections());
				}else {
					List<TomcatInfo> tomcatInfoList = tomcatInfoDao.getTomcatInfoList(serverInfo.getIp(), serverInfo.getCode());
					List<Integer> conns = new ArrayList<Integer>();
					for (TomcatInfo tomcatInfo : tomcatInfoList) {
						conns.add(tomcatInfo.getConnections());
					}
					map.put(serverInfo.getName(), conns);
					maxConnMap.put(serverInfo.getName(), tomcatInfoDao.getNewTomcatInfo(serverInfo.getIp(), serverInfo.getCode()).getMaxConnections());
				}
			}else if (serverInfo.getCode().equals("NGINX")) {
				if(serverInfo.getIp().contains(",")){
					String[] split = serverInfo.getIp().split(",");
					List<NginxInfo> nginxInfoList = nginxInfoDao.getNginxInfoList(split[0]);
					Long maxTime = nginxInfoList.get(nginxInfoList.size()-1).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						List<NginxInfo> nginxInfos = nginxInfoDao.getNginxInfoList(split[i]);
						Long time = nginxInfos.get(nginxInfos.size()-1).getTime();
						if(maxTime<time){
							maxTime = time;
							j=i;
						}
					}	
					List<NginxInfo> nginxInfos = nginxInfoDao.getNginxInfoList(split[j]);
					List<Integer> conns = new ArrayList<Integer>();
					for (NginxInfo nginxInfo : nginxInfos) {
						conns.add(nginxInfo.getConnections());
					}
					map.put(serverInfo.getName(), conns);
					maxConnMap.put(serverInfo.getName(), nginxInfoDao.getNewNginxInfo(split[j]).getMaxConnections());
				}else {
					List<NginxInfo> nginxInfos = nginxInfoDao.getNginxInfoList(serverInfo.getIp());
					List<Integer> conns = new ArrayList<Integer>();
					for (NginxInfo nginxInfo : nginxInfos) {
						conns.add(nginxInfo.getConnections());
					}
					map.put(serverInfo.getName(), conns);
					maxConnMap.put(serverInfo.getName(), nginxInfoDao.getNewNginxInfo(serverInfo.getIp()).getMaxConnections());
				}
			}else if (serverInfo.getCode().equals("MYSQL")) {
				if (serverInfo.getIp().contains(",")) {
					String[] split = serverInfo.getIp().split(",");
					List<MysqlInfo> mysqlInfoList = mysqlInfoDao.getMysqlInfoList(split[0]);
					Long maxTime = mysqlInfoList.get(mysqlInfoList.size()-1).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						List<MysqlInfo> mysqlInfos = mysqlInfoDao.getMysqlInfoList(split[i]);
						Long time = mysqlInfos.get(mysqlInfos.size()-1).getTime();
						if (maxTime<time) {
							maxTime = time;
							j = i;
						}
					}
					List<MysqlInfo> mysqlInfos = mysqlInfoDao.getMysqlInfoList(split[j]);
					List<Integer> conns = new ArrayList<Integer>();
					for (MysqlInfo mysqlInfo : mysqlInfos) {
						conns.add(mysqlInfo.getConnections());
					}
					map.put(serverInfo.getName(), conns);
					maxConnMap.put(serverInfo.getName(), mysqlInfoDao.getNewMysqlInfo(split[j]).getMaxConnections());
				}else {
					List<MysqlInfo> mysqlInfos = mysqlInfoDao.getMysqlInfoList(serverInfo.getIp());
					List<Integer> conns = new ArrayList<Integer>();
					for (MysqlInfo mysqlInfo : mysqlInfos) {
						conns.add(mysqlInfo.getConnections());
					}
					map.put(serverInfo.getName(), conns);
					maxConnMap.put(serverInfo.getName(), mysqlInfoDao.getNewMysqlInfo(serverInfo.getIp()).getMaxConnections());
				}
			}else if (serverInfo.getCode().equals("ORACLE")) {
				if (serverInfo.getIp().contains(",")){
					String[] split = serverInfo.getIp().split(",");
					List<OracleInfo> oracleInfoList = oracleInfoDao.getOracleInfoList(split[0]);
					Long maxTime = oracleInfoList.get(oracleInfoList.size()-1).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						List<OracleInfo> oracleInfos = oracleInfoDao.getOracleInfoList(split[i]);
						Long time = oracleInfos.get(oracleInfos.size()-1).getTime();
						if(maxTime<time && time !=null){
							maxTime = time;
							j = i;
						}else{
							break;
						}
					}
					List<OracleInfo> oracleInfos = oracleInfoDao.getOracleInfoList(split[j]);
					List<Integer> conns = new ArrayList<Integer>();
					for (OracleInfo oracleInfo : oracleInfos) {
						conns.add(oracleInfo.getConnections());
						timeList.add(oracleInfo.getTime());
					}
					map.put(serverInfo.getName(), conns);
					maxConnMap.put(serverInfo.getName(), oracleInfoDao.getNewOracleInfo(split[j]).getMaxConnections());
				}else {
					List<OracleInfo> oracleInfos = oracleInfoDao.getOracleInfoList(serverInfo.getIp());
					List<Integer> conns = new ArrayList<Integer>();
					for (OracleInfo oracleInfo : oracleInfos) {
						conns.add(oracleInfo.getConnections());
						timeList.add(oracleInfo.getTime());
					}
					map.put(serverInfo.getName(), conns);
					maxConnMap.put(serverInfo.getName(), oracleInfoDao.getNewOracleInfo(serverInfo.getIp()).getMaxConnections());
				}
			}
		}
		List<Object> list = new ArrayList<Object>();
		map.put("time", timeList);
		//System.out.println("getAllConnNumList:"+timeList);
		list.add(map);
		list.add(maxConnMap);
		ResponseData responseData = new ResponseData(true, list);
		return responseData;
	}
	
	/**
	 * 得到所有的应用连接数
	 * @return
	 */
	@RequestMapping(value = "/getNewAllConnNum", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getNewAllConnNum(){
		ServerDao serverDao = new ServerDao();
		TomcatInfoDao tomcatInfoDao = new TomcatInfoDao();
		NginxInfoDao nginxInfoDao = new NginxInfoDao();
		MysqlInfoDao mysqlInfoDao = new MysqlInfoDao();
		OracleInfoDao oracleInfoDao = new OracleInfoDao();
		List<ServerInfo> serverInfos = serverDao.getAllServerInfo();
		List<Long> timeList = new ArrayList<Long>();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> maxConnMap = new HashMap<String, Object>();
		for (ServerInfo serverInfo : serverInfos) {
//			System.out.println(serverInfo.getIp());
			if (serverInfo.getCode().contains("TCM")) {
				if(serverInfo.getIp().contains(",")){
					String[] split = serverInfo.getIp().split(",");
					Long maxTime = tomcatInfoDao.getNewTomcatInfo(split[0], serverInfo.getCode()).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						Long time = tomcatInfoDao.getNewTomcatInfo(split[i], serverInfo.getCode()).getTime();
						if(maxTime<time){
							maxTime=time;
							j=i;
						}
					}
					timeList.add(maxTime);
					TomcatInfo newTomcatInfo = tomcatInfoDao.getNewTomcatInfo(split[j], serverInfo.getCode());
					map.put(serverInfo.getName(), newTomcatInfo.getConnections());
					maxConnMap.put(serverInfo.getName(), newTomcatInfo.getMaxConnections());
				}else {
					TomcatInfo newTomcatInfo = tomcatInfoDao.getNewTomcatInfo(serverInfo.getIp(), serverInfo.getCode());
					Integer connections = newTomcatInfo.getConnections();
					timeList.add(newTomcatInfo.getTime());
					map.put(serverInfo.getName(), connections);	
					maxConnMap.put(serverInfo.getName(), newTomcatInfo.getMaxConnections());
				}
			}else if (serverInfo.getCode().equals("NGINX")) {
				if(serverInfo.getIp().contains(",")){
					String[] split = serverInfo.getIp().split(",");
					Long maxTime = nginxInfoDao.getNewNginxInfo(split[0]).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						Long time = nginxInfoDao.getNewNginxInfo(split[0]).getTime();
						if(maxTime<time){
							maxTime = time;
							j=i;
						}
					}
					NginxInfo newNginxInfo = nginxInfoDao.getNewNginxInfo(split[j]);
					map.put(serverInfo.getName(), newNginxInfo.getConnections());
					maxConnMap.put(serverInfo.getName(), newNginxInfo.getMaxConnections());
					timeList.add(maxTime);
				}else {
					NginxInfo newNginxInfo = nginxInfoDao.getNewNginxInfo(serverInfo.getIp());	
					map.put(serverInfo.getName(), newNginxInfo.getConnections());
					maxConnMap.put(serverInfo.getName(), newNginxInfo.getMaxConnections());
					timeList.add(newNginxInfo.getTime());
				}
			}else if (serverInfo.getCode().equals("MYSQL")) {
				if (serverInfo.getIp().contains(",")) {
					String[] split = serverInfo.getIp().split(",");
					Long maxTime = mysqlInfoDao.getNewMysqlInfo(split[0]).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						Long time = mysqlInfoDao.getNewMysqlInfo(split[i]).getTime();
						if (maxTime<time) {
							maxTime = time;
							j = i;
						}
					}
					MysqlInfo newMysqlInfo = mysqlInfoDao.getNewMysqlInfo(split[j]);
					map.put(serverInfo.getName(), newMysqlInfo.getConnections());
					maxConnMap.put(serverInfo.getName(), newMysqlInfo.getMaxConnections());
					timeList.add(maxTime);
				}else {
					MysqlInfo newMysqlInfo = mysqlInfoDao.getNewMysqlInfo(serverInfo.getIp());
					map.put(serverInfo.getName(), newMysqlInfo.getConnections());
					maxConnMap.put(serverInfo.getName(), newMysqlInfo.getMaxConnections());
					timeList.add(newMysqlInfo.getTime());
				}
			}else if (serverInfo.getCode().equals("ORACLE")) {
				if (serverInfo.getIp().contains(",")){
					String[] split = serverInfo.getIp().split(",");
					Long maxTime = oracleInfoDao.getNewOracleInfo(split[0]).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						Long time = oracleInfoDao.getNewOracleInfo(split[i]).getTime();
						if(maxTime<time){
							maxTime = time;
							j = i;
						}
					}
					OracleInfo newOracleInfo = oracleInfoDao.getNewOracleInfo(split[j]);
					map.put(serverInfo.getName(), newOracleInfo.getConnections());
					maxConnMap.put(serverInfo.getName(), newOracleInfo.getMaxConnections());
					timeList.add(maxTime);
				}else {
					OracleInfo newOracleInfo = oracleInfoDao.getNewOracleInfo(serverInfo.getIp());
					map.put(serverInfo.getName(), newOracleInfo.getConnections());
					maxConnMap.put(serverInfo.getName(), newOracleInfo.getMaxConnections());
					timeList.add(newOracleInfo.getTime());
				}
			}
		}
		//System.out.println(timeList.get(0));
		Long maxtime = timeList.get(0);
		for(int i=1; i<timeList.size(); i++){
			if(maxtime<timeList.get(i) && timeList.get(i) != null ){
				maxtime=timeList.get(i);
			}else{
				break;
			}
		}
		map.put("time", maxtime);
		List<Object> list = new ArrayList<Object>();
		list.add(map);
		list.add(maxConnMap);
		ResponseData responseData = new ResponseData(true, list);
		return responseData;
	}
	/**
	 * 得到所有的应用状态（服务状态端口状态）
	 * @return
	 */
	@RequestMapping(value = "/getNewAllAppStatus", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData getNewAllAppStatus(){
		ServerDao serverDao = new ServerDao();
		TomcatInfoDao tomcatInfoDao = new TomcatInfoDao();
		NginxInfoDao nginxInfoDao = new NginxInfoDao();
		MysqlInfoDao mysqlInfoDao = new MysqlInfoDao();
		OracleInfoDao oracleInfoDao = new OracleInfoDao();
		List<ServerInfo> serverInfos = serverDao.getAllServerInfo();
		Map<String, Object> statusmap = new HashMap<String, Object>();
		Map<String, Object> SQlmap = new HashMap<String, Object>();
		Map<String, Object> SQlPortmap = new HashMap<String, Object>();
		Map<String, Object> portStatusMap = new HashMap<String, Object>();
		for (ServerInfo serverInfo : serverInfos) {
			if (serverInfo.getCode().contains("TCM")) {
				if(serverInfo.getIp().contains(",")){
					String[] split = serverInfo.getIp().split(",");
					Long maxTime = tomcatInfoDao.getNewTomcatInfo(split[0], serverInfo.getCode()).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						Long time = tomcatInfoDao.getNewTomcatInfo(split[i], serverInfo.getCode()).getTime();
						if(maxTime<time){
							maxTime=time;
							j=i;
						}
					}
					TomcatInfo newTomcatInfo = tomcatInfoDao.getNewTomcatInfo(split[j], serverInfo.getCode());
					statusmap.put(serverInfo.getName(), newTomcatInfo.getStatus());
					portStatusMap.put(serverInfo.getName(), newTomcatInfo.getPortStatus());
				}else {
					TomcatInfo newTomcatInfo = tomcatInfoDao.getNewTomcatInfo(serverInfo.getIp(), serverInfo.getCode());
					statusmap.put(serverInfo.getName(), newTomcatInfo.getStatus());
					portStatusMap.put(serverInfo.getName(), newTomcatInfo.getPortStatus());
				}
			}else if (serverInfo.getCode().equals("NGINX")) {
				if(serverInfo.getIp().contains(",")){
					String[] split = serverInfo.getIp().split(",");
					Long maxTime = nginxInfoDao.getNewNginxInfo(split[0]).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						Long time = nginxInfoDao.getNewNginxInfo(split[0]).getTime();
						if(maxTime<time){
							maxTime = time;
							j=i;
						}
					}
					NginxInfo newNginxInfo = nginxInfoDao.getNewNginxInfo(split[j]);
					statusmap.put(serverInfo.getName(), newNginxInfo.getStatus());
					portStatusMap.put(serverInfo.getName(), newNginxInfo.getPortStatus());
				}else {
					NginxInfo newNginxInfo = nginxInfoDao.getNewNginxInfo(serverInfo.getIp());	
					statusmap.put(serverInfo.getName(), newNginxInfo.getStatus());
					portStatusMap.put(serverInfo.getName(), newNginxInfo.getPortStatus());
				}
			}else if (serverInfo.getCode().equals("MYSQL")) {
				if (serverInfo.getIp().contains(",")) {
					String[] split = serverInfo.getIp().split(",");
					Long maxTime = mysqlInfoDao.getNewMysqlInfo(split[0]).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						Long time = mysqlInfoDao.getNewMysqlInfo(split[i]).getTime();
						if (maxTime<time) {
							maxTime = time;
							j = i;
						}
					}
					MysqlInfo newMysqlInfo = mysqlInfoDao.getNewMysqlInfo(split[j]);
					SQlmap.put(serverInfo.getName(), newMysqlInfo.getStatus());
					SQlPortmap.put(serverInfo.getName(), newMysqlInfo.getPortStatus());
					
				}else {
					MysqlInfo newMysqlInfo = mysqlInfoDao.getNewMysqlInfo(serverInfo.getIp());
					SQlmap.put(serverInfo.getName(), newMysqlInfo.getStatus());
					SQlPortmap.put(serverInfo.getName(), newMysqlInfo.getPortStatus());
				}
			}else if (serverInfo.getCode().equals("ORACLE")) {
				if (serverInfo.getIp().contains(",")){
					String[] split = serverInfo.getIp().split(",");
					Long maxTime = oracleInfoDao.getNewOracleInfo(split[0]).getTime();
					int j = 0;
					for (int i = 1; i < split.length; i++) {
						Long time = oracleInfoDao.getNewOracleInfo(split[i]).getTime();
						if(maxTime<time){
							maxTime = time;
							j = i;
						}
					}
					OracleInfo newOracleInfo = oracleInfoDao.getNewOracleInfo(split[j]);
					SQlmap.put(serverInfo.getName(), newOracleInfo.getStatus());
					SQlPortmap.put(serverInfo.getName(), newOracleInfo.getPortStatus());
				}else {
					OracleInfo newOracleInfo = oracleInfoDao.getNewOracleInfo(serverInfo.getIp());
					SQlmap.put(serverInfo.getName(), newOracleInfo.getStatus());
					SQlPortmap.put(serverInfo.getName(), newOracleInfo.getPortStatus());
				}
			}
		}
		List<Object> list = new ArrayList<Object>();
		statusmap.put("数据库", SQlmap);
		portStatusMap.put("数据库", SQlPortmap);
		list.add(statusmap);
		list.add(portStatusMap);
		ResponseData responseData = new ResponseData(true, list);
		return responseData;
	}
}
